﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Rpt;
using nERP.Model;
using nERP.Model.Po;
using nERP.Model.So;
using nERP.Model.Fin;

namespace nERP.ViewModel.Rpt.DashboardVMs
{
    public partial class DashboardListVM : BasePagedListVM<Dashboard_View, DashboardSearcher>
    {

        protected override IEnumerable<IGridColumn<Dashboard_View>> InitGridHeader()
        {
            return new List<GridColumn<Dashboard_View>>{

                this.MakeGridHeader(x => x.Dashboard_Monthly).SetTitle(@Localizer["Page.月份"].Value),
                this.MakeGridHeader(x => x.Dashboard_PoAmount).SetTitle(@Localizer["Page.采购总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_PoAmounted).SetTitle(@Localizer["Page.已签采购总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_SoAmount).SetTitle(@Localizer["Page.销售总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_SoAmounted).SetTitle(@Localizer["Page.已签销售总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_PayAmount).SetTitle(@Localizer["Page.付款总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_PayAmounted).SetTitle(@Localizer["Page.已签付款总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_ReceivableAmount).SetTitle(@Localizer["Page.收款总额"].Value),
                this.MakeGridHeader(x => x.Dashboard_ReceivableAmounted).SetTitle(@Localizer["Page.已签收款总额"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<Dashboard_View> GetSearchQuery()
        {
            var query = DC.Set<Dashboard>()

                .CheckEqual(Searcher.Monthly, x => x.Monthly)
                .Select(x => new Dashboard_View
                {
                    ID = x.ID,

                    Dashboard_Monthly = x.Monthly,
                    Dashboard_PoAmount = DC.Set<Buy>().Sum(z => z.TotalPrice),
                    Dashboard_PoAmounted = DC.Set<Buy>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    Dashboard_SoAmount = DC.Set<Sale>().Sum(z => z.TotalPrice),
                    Dashboard_SoAmounted = DC.Set<Sale>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    Dashboard_PayAmount = DC.Set<Payment>().Sum(z => z.TotalPrice),
                    Dashboard_PayAmounted = DC.Set<Payment>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    Dashboard_ReceivableAmount = DC.Set<Receivable>().Sum(z => z.TotalPrice),
                    Dashboard_ReceivableAmounted = DC.Set<Receivable>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                })
                .OrderBy(x => x.ID);
            return query;
        }


    }
    public class Dashboard_View : Dashboard
    {

        public string Dashboard_Monthly { get; set; }
        public decimal? Dashboard_PoAmount { get; set; }
        public decimal? Dashboard_PoAmounted { get; set; }
        public decimal? Dashboard_SoAmount { get; set; }
        public decimal? Dashboard_SoAmounted { get; set; }
        public decimal? Dashboard_PayAmount { get; set; }
        public decimal? Dashboard_PayAmounted { get; set; }
        public decimal? Dashboard_ReceivableAmount { get; set; }
        public decimal? Dashboard_ReceivableAmounted { get; set; }

    }

}